package com.imageim.yimirror.common.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.DecelerateInterpolator;

import com.imageim.yimirror.R;
import com.imageim.yimirror.common.ui.MultiResolutionUtil;
import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;

/**
 * 弹出框
 * Created by Bruce.Zhou on 2016/3/10.
 */
public class PromptDialog extends Dialog {

    protected View contentView;
    private boolean isOutsideCanelable;
    //0=WRAP_CONTENT    1=MATCH_PARENT
    private static int WINDOW_MANAGE_STYLE = 0;
    private static final int WRAP_CONTENT = 0;
    private static final int MATCH_PARENT = 1;

    public interface BindViewListener {
        void onBindView(View contentView);
    }


    public PromptDialog bindView(int layoutId, BindViewListener bindViewListener) {
        if (bindViewListener == null) {
            return this;
        }
        contentView = LayoutInflater.from(getContext()).inflate(layoutId, null);
        MultiResolutionUtil.adapter(contentView);
        bindViewListener.onBindView(contentView);
        return this;
    }

    public PromptDialog(Context context) {
        super(context, R.style.full_screen_dialog);
        hideBottomUIMenu();
    }

    /**
     * 隐藏虚拟按键，并且全屏
     */
    protected void hideBottomUIMenu() {
        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }
    }


    public PromptDialog(Context context, int themeResId) {
        super(context, themeResId);
        hideBottomUIMenu();
    }

    public PromptDialog(Context context, int themeResId, int windowManageStyle) {
        super(context, themeResId);
        hideBottomUIMenu();
        WINDOW_MANAGE_STYLE = windowManageStyle;
    }

    protected PromptDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        hideBottomUIMenu();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (contentView != null) {
            setContentView(contentView);
        }

        try {
            WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
            lp.copyFrom(getWindow().getAttributes());
            if (WINDOW_MANAGE_STYLE == WRAP_CONTENT) {
                lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
                lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            } else {
                lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
                lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
            }
            getWindow().setAttributes(lp);
        } catch (Exception e) {
            e.printStackTrace();
        }

        setCanceledOnTouchOutside(isOutsideCanelable);
        getWindow().setGravity(Gravity.CENTER);

        if (contentView != null) {
            AnimatorSet set = new AnimatorSet();
            ObjectAnimator scaleX = ObjectAnimator.ofFloat(contentView, "scaleX", 0, 1);
            ObjectAnimator scaleY = ObjectAnimator.ofFloat(contentView, "scaleY", 0, 1);
            set.playTogether(scaleX, scaleY);
            set.setDuration(500);
            set.setInterpolator(new DecelerateInterpolator());
            set.setTarget(contentView);
            set.start();
        }
    }


    public void setGravity(int gravity) {
        this.getWindow().setGravity(gravity);
    }

    public View getContentView() {
        return this.contentView;
    }

    public void setOutsideCanelable(boolean canelable) {
        isOutsideCanelable = canelable;
    }
}
